<?php

namespace Itwmw\Attributes\Validate\Provider;

use Itwmw\Validate\Support\Storage\ValidateConfig;
use Itwmw\Validation\Factory;
use Itwmw\Validation\Validator;

/**
 * 内部使用验证
 *
 * @internal
 */
class InternalValidator extends Validator
{
    protected $rules = [];

    public function newPasses(string $attribute): bool
    {
        $rules = $this->rules[$attribute] ?? [];
        foreach ($rules as $rule) {
            $this->validateAttribute($attribute, $rule);
        }

        return true;
    }

    public static function make(array $data, array $rules, array $messages = [], array $attributes = []): static
    {
        $config     = ValidateConfig::instance();
        $verifier   = $config->getPresenceVerifier();
        $translator = $config->getTranslator() ?: Factory::makeTranslator();

        $validator = new static($translator, $data, $rules, $messages, $attributes);
        if (!is_null($verifier)) {
            $validator->setPresenceVerifier($verifier);
        }

        return $validator;
    }
}
